package com.dove.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dove.common.Constants;
import com.dove.common.Result;
import com.dove.entity.Dict;
import com.dove.entity.Menu;
import com.dove.mapper.DictMapper;
import com.dove.service.IMenuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author dove
 * @since 2022-03-26
 */
@RestController
@RequestMapping("/menus")
public class MenuController {
    @Resource
    private IMenuService menuService;

    @Resource
    private DictMapper dictMapper;

    @PostMapping
    public Result save(@RequestBody Menu menu) {
        boolean b = menuService.saveOrUpdate(menu);
        if (b) {
            return Result.success("保存成功");
        }
        return Result.error("保存失败");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean b = menuService.removeById(id);
        if (b) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    //删除选中
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        boolean b = menuService.removeByIds(ids);
        if (b) {
            return Result.success("批量删除成功");
        }
        return Result.error("批量删除失败");
    }

    @GetMapping
    public Result findAll(@RequestParam(defaultValue = "") String name) {
        List<Menu> parentNode = menuService.findMenus(name);
        for (Menu menu : parentNode) {
            List<Menu> children = menu.getChildren();

        }
        return Result.success(menuService.findMenus(name));
        /*if (list == null) {
            return Result.error("查询全部失败");
        }
        return Result.success(parentNodes);//bug 只返回父菜单，查询不返回子菜单*/
        //return Result.success(list);
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        Menu menu = menuService.getById(id);
        if (menu == null) {
            return Result.error("id查询失败");
        }
        return Result.success(menu);
    }

    @GetMapping("/ids")
    public Result findAllIds() {
        return Result.success(menuService.list().stream().map(Menu::getId));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String name) {
        IPage<Menu> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Menu> qw = new QueryWrapper<>();
        if (!"".equals(name)) {
            qw.like("name", name);
        }
        return Result.success(menuService.page(page, qw));
    }

    @GetMapping("/icons")
    public Result findIcons() {
        QueryWrapper<Dict> qw = new QueryWrapper<>();
        qw.eq("type", Constants.DICT_TYPE_ICON);
        List<Dict> dicts = dictMapper.selectList(qw);
        if (dicts == null) {
            return Result.error("查询icon失败");
        }
        return Result.success(dicts);
    }
}
